$(function () {
    initialPage();
    getGrid();
});

function initialPage() {
    $(window).resize(function () {
        TreeGrid.table.resetHeight({height: $(window).height() - 100});
    });
}

function getGrid() {
    var colunms = TreeGrid.initColumn();
    var table = new TreeTable(TreeGrid.id, '../category/queryAll', colunms);
    table.setExpandColumn(2);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentCode");
    table.setExpandAll(false);
    table.setHeight($(window).height() - 100);
    table.init();
    TreeGrid.table = table;
}

var TreeGrid = {
    id: "jqGrid",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TreeGrid.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', align: 'id', width: '50px'},
        {title: '分类编码', field: 'code', align: 'center', valign: 'middle', width: '50px'},
        {title: '分类名称', field: 'name', align: 'center', valign: 'middle', width: '80px'},
        /*{title: '描述', field: 'frontDesc', align: 'center', valign: 'middle', width: '150px'},
        {title: '首页展示', field: 'showIndex', align: 'center', valign: 'middle', width: '50px'},*/
       /* {title: '父类型', field: 'parent.name', align: 'center', valign: 'middle', width: '50px'},*/
        {
            title: '显示',
            field: 'status',
            align: 'center',
            valign: 'middle',
            width: '50px',
            formatter: function (item, index) {
                return transIsNot(item.status)
            }
        },
        /*{title: '状态', field: 'status', align: 'center', valign: 'middle', width: '50px'},*/
        {title: '类型', field: 'type', align: 'center', valign: 'middle', width: '50px',
            formatter: function (item) {
                if (item.type == 1) {
                    return '<span class="label label-success">枝</span>';
                }
                return '<span class="label label-success">叶</span>';

            }
        }]
    return columns;
};

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        parentShow:false,
        title: null,
        disabled:"",
        category: {status:1, type: 1, name:null, code:null,parentCode:0,parent:null},
        ruleValidate: {
            name: [
                {required: true, message: '分类名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
        categoryList: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            var Id = 0;
            var categoryId = TreeGrid.table.getSelectedRow();
            if(categoryId.length != 0 ){ //点击某个节点来进行添加
                //alert("categoryId:"+categoryId[0].id);
                Id = categoryId[0].id;//用作下一级的父类别id
                $.get("../category/info/" + Id, function (r) {
                    vm.category = {
                        status: 1,
                        type: 0,
                        code: null,
                        parentCode: r.category.code,
                        parent: r.category
                    };

                });
            }else {  //不选中节点来添加的
                vm.category={
                    status:1, type: 1, code:null, parentCode:null, parent:null
                }
            }
            vm.disabled = true;
            vm.parentShow = true;
            vm.showList = false;
            vm.title = "新增";
            //vm.category = {status:1, type: 1,code:null};
            //this.getParentCategory();
        },
        update: function (event) {
            var parentId = 0;
            var categoryId = TreeGrid.table.getSelectedRow();
            if (categoryId.length == 0) {
                iview.Message.error("请选择一条记录");
                return;
            }
            //alert("categoryId:"+categoryId[0].id);
            parentId = categoryId[0].id;//用作下一级的父类别id
            $.get("../category/info/" + parentId, function (r) {   //拿上一级
                if(r.category.parent != null){
                    vm.category ={  status: r.category.status,
                        type:  r.category.type,
                        name:  r.category.name,
                        code:r.category.code,
                        parentCode:r.category.parent.code,
                        parent:r.category.parent,
                        id:r.category.id,
                        wapBannerUrl:r.category.wapBannerUrl
                    };
                }else{
                    vm.getParentCategory(r);
                    /*$.get("../category/getCategorySelect", function (re) {
                        var firstCategory = re.list;
                        var size = re.list.length;
                        console.log(size);
                        console.log(firstCategory[size-1].name);
                        vm.category={status:r.category.status,
                            type: r.category.type,
                            name:  r.category.name,
                            code:r.category.code,
                            parentCode:firstCategory[size-1].code,
                            parent:firstCategory[size-1]};
                    });*/

                }

            });
            vm.disabled = true;
            vm.parentShow = true;
            vm.showList = false;
            vm.title = "修改";
            //vm.getInfo(id[0].id);

        },
        getParentCategory: function (r) {
            /*$.get("../category/getCategorySelect", function (r) {
                vm.categoryList = r.list;
                console.log("categoryList:"+vm.categoryList);
            });*/
            $.get("../category/getCategorySelect", function (re) {
                var firstCategory = re.list;
                var size = re.list.length;
                console.log(size);
                console.log(firstCategory[size - 1].name);
                vm.category = {
                    status: r.category.status,
                    type: r.category.type,
                    name: r.category.name,
                    code: r.category.code,
                    parentCode: firstCategory[size - 1].code,
                    parent: firstCategory[size - 1],
                    id:r.category.id,
                    wapBannerUrl:r.category.wapBannerUrl
                };
            });
        },
        saveOrUpdate: function (event) {
            /*var url = vm.category.id == null ? "../category/save" : "../category/update";*/
            var url =" ../category/saveOrUpdate";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.category),
                success: function (r) {
                    if (r.status === "00000") {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var id = TreeGrid.table.getSelectedRow(), ids = [];
            if (id.length == 0) {
                iview.Message.error("请选择一条记录");
                return;
            }
            $.each(id, function (idx, item) {
                ids[idx] = item.id;
            });
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../category/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.status == "00000") {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../category/info/" + id, function (r) {
                vm.category = r.category;
                console.log("getInfo:"+vm.category.parent.name);
            });
        },
        reload: function (event) {
            vm.showList = true;
            TreeGrid.table.refresh();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccessWapBannerUrl: function (res, file) {
            vm.category.wapBannerUrl = file.response.url;
        },
        eyeImageWapBannerUrl: function () {
            var url = vm.category.wapBannerUrl;
            eyeImage(url);
        },
    }
});